<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form
      ref="form"
      label-width="140px"
      :model="formData"
      :rules="formRules"
    >
      <el-row type="flex" justify="space-eventy">
        <el-form-item label="管理员账号" prop="login_name">
          <el-input v-model="formData.login_name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="管理员姓名" prop="name">
          <el-input v-model="formData.name" :rows="3" placeholder="请输入" />
        </el-form-item>
      </el-row>
      <el-row type="flex" justify="space-eventy">
        <el-form-item label="管理员邮箱" prop="email">
          <el-input v-model="formData.email" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="管理员手机" prop="phone">
          <el-input v-model="formData.phone" :rows="3" placeholder="请输入" />
        </el-form-item>
      </el-row>
      <el-row type="flex" justify="space-eventy">
        <el-form-item label="管理员密码" prop="password">
          <el-input v-model="formData.password" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="账号状态" prop="status">
          <template>
            <el-switch
              v-model="formData.status"
              :active-value="1"
              :inactive-value="0"
              inactive-text="禁用"
              active-text="启动"
              @change="sysChange"
            />
          </template>
        </el-form-item>
      </el-row>
      <el-form-item label="角色" prop="role_id">
        <el-checkbox-group v-model="checkList">
          <el-checkbox
            v-for="item in roleName"
            :key="item.id"
            :label="item.id"
            :value="item.id"
          >{{ item.name }}</el-checkbox>
        </el-checkbox-group>

      </el-form-item>

      <el-row type="flex" justify="center">
        <el-button
          type="primary"
          size="small"
          @click="onSubmit"
        >提交</el-button>
        <el-button size="small" @click="dialogShow = false">关闭</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addSysUserInfoApi, updateSysUserInfoApi, getSysRoleListApi } from '@/api/sys_user'
// import { getSysRoleListApi } from '@/api/sys_role'
export default {
  name: 'AddFormUserSys',
  data() {
    return {
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        email: '',
        login_name: '',
        name: '',
        password: '',
        phone: '',
        role_id: [],
        status: '0'
      },
      value: '',
      checkList: [],
      // checkListed: [],

      // 表单验证规则
      formRules: {
        login_name: [{ required: true, message: '请输入', trigger: 'blur' }],
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        password: [{ required: true, message: '请输入', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入', trigger: 'blur' }],
        // role_id: [{ required: true, message: '请输入', trigger: 'blur' }],
        email: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      roleName: []
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  created() {
    this.getRole()
  },
  methods: {
    sysChange(val) {
      val ? this.formData.status = '1' : this.formData.status = '0'
    },
    async getRole() {
      const res = await getSysRoleListApi({
        page: 1,
        limit: 10000
      })
      // const roleName = []
      res.data.data.forEach((item, index) => {
        this.roleName.push({
          id: item.id,
          name: item.name
        })
      })
      console.log(this.roleName)
      // console.log(this.roleName)
    },
    // 提交行为
    async onSubmit() {
      // 表单的校验
      await this.$refs.form.validate()
      // this.formData.role_id = this.checkList.map(item => item.id)
      // 加工处理表单数据(拷贝一份再加工)
      this.formData.role_id = [...this.checkList]
      // 请求的发生
      this.formData.id
        ? await updateSysUserInfoApi(this.formData)
        : await addSysUserInfoApi(this.formData)
      // 提示效果
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      // 关闭弹框
      this.dialogShow = false
      // 列表更新
      this.$emit('updateList')
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        email: '',
        login_name: '',
        name: '',
        password: '',
        phone: '',
        role_id: [],
        status: ''
      }
      // 移除校验
      this.$refs.form.clearValidate()
      this.checkList = []
    }
  }
}
</script>
<style scoped></style>
